package com.api.sw.duban.action;

import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.api.sw.duban.util.DubUtil;
import com.api.sw.duban.util.LogTool;
import com.cloudstore.eccom.constant.WeaBoolAttr;
import com.cloudstore.eccom.pc.table.WeaTable;
import com.cloudstore.eccom.pc.table.WeaTableCheckboxpopedom;
import com.cloudstore.eccom.pc.table.WeaTableColumn;
import com.cloudstore.eccom.result.WeaResultMsg;
import weaver.conn.RecordSet;
import weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DubanyBBAction {
    private final static LogTool log = new LogTool("/log/duban/",false);
    public Map<String, Object> getCondition(Map<String,Object> params, User user) {

        Map<String, Object> apidatas = new HashMap<String, Object>();
        //角色判断参考代码
//        if(!HrmUserVarify.checkUserRight("LogView:View", user)){
//            apidatas.put("hasRight", false);
//            return apidatas;
//        }

        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }

        apidatas.put("hasRight", true);
        //0:人经办统计  1:单位承办统计 2:督查事项统计
        String condtiontype= Util.null2String(params.get("condtiontype"));
        /**
         * 高级搜索条件
         */
        ConditionFactory conditionFactory = new ConditionFactory(user);

        //条件组
        List<SearchConditionGroup> addGroups = new ArrayList<SearchConditionGroup>();

        List<SearchConditionItem> conditionItems = new ArrayList<SearchConditionItem>();
        //人员
        SearchConditionItem ry = conditionFactory.createCondition(ConditionType.BROWSER,502327, "ry","17");
        ry.setLabel("经办人");
        if("0".equals(condtiontype)){
            conditionItems.add(ry);
        }

        //主办单位
        SearchConditionItem zbdw = conditionFactory.createCondition(ConditionType.BROWSER,502327, "dw","142");
        zbdw.setLabel("单位");

        if("1".equals(condtiontype)){
            conditionItems.add(zbdw);
        }



        //办理状态
        SearchConditionItem status = conditionFactory.createCondition(ConditionType.SELECT,502327,"lx");
        List<SearchConditionOption> typeOptions = new ArrayList <SearchConditionOption>();  //设置选项值
        typeOptions.add(new SearchConditionOption("-0",""));
        typeOptions.add(new SearchConditionOption("0","常规督查"));
        typeOptions.add(new SearchConditionOption("1","决策督查"));
        status.setOptions(typeOptions);
        status.setLabel("类型");

        if("1".equals(condtiontype)){
            conditionItems.add(status);
        }
        //反馈时限
        SearchConditionItem fksx = conditionFactory.createCondition(ConditionType.DATE,502327, new String[]{"sj", "sjfrom", "sjto"});
        fksx.setLabel("日期");
        conditionItems.add(fksx);

        addGroups.add(new SearchConditionGroup("查询条件",true,conditionItems));


        apidatas.put("condition",addGroups);

        return apidatas;

    }
    /*
        经办人 报表
     */
    public Map<String, Object> getSingleTablelist(Map<String,Object> params, User user) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }
        try {
            if(params!=null){
                log.writeLog(params.toString());
            }
            String sqlfield="";
            String frist=Util.null2String(params.get("frist")).trim();//0:代表第一次加载
            String ry=(String)Util.null2String(params.get("ry")).trim() ;//人员
            if("".equals(ry)){
                sqlfield=sqlfield+"-1,";
            }else{
                sqlfield=sqlfield+ry+",";
            }

            //日期
            String fksx=(String)Util.null2String(params.get("fksx")) ;//反馈时限
            String fksxfrom=(String)Util.null2String(params.get("fksxfrom")) ;//反馈时限from
            String fksxto=(String)Util.null2String(params.get("fksxto")) ;//反馈时限to



            //1 今天:2本周:3本月:4本季度:5本年:6时间区间:7上年:8:上个月

            //返回消息结构体
            WeaResultMsg result = new WeaResultMsg(false);
            RecordSet re=new RecordSet();
            String DBType=re.getDBType();
            log.writeLog("DBType"+DBType);
            String pageID = "17ecc14c-9d64-4a43-81a4-7b7c6f7928571113211";
            String pageUid = pageID + "_" + user.getUID();
            String pageSize = PageIdConst.getPageSize(pageID, user.getUID());

            String sqlwhere ="  ";
            if(!"".equals(ry)){
                if(ry.endsWith(",")){
                    ry=ry.substring(0,ry.length()-1);
                }
                if("sqlserver".equals(DBType)){
                    sqlwhere="    and t.modedatacreater in ("+ry+") ";
                }else{
                    sqlwhere="    and t.modedatacreater in ("+ry+") ";
                }
            }
            if(!"".equals(fksx)){
                if("6".equals(fksx)){
                    sqlfield=sqlfield+"6,";
                    if(!"".equals(fksxfrom)){
                        sqlfield=sqlfield+fksxfrom+",";
                        sqlwhere=sqlwhere+" and substr(t.modedatacreatedate,0,10)>='"+fksxfrom+"'";
                    }else{
                        sqlfield=sqlfield+"-1,";
                    }
                    if(!"".equals(fksxto)){
                        sqlfield=sqlfield+fksxto+",";
                        sqlwhere=sqlwhere+" and substr(t.modedatacreatedate,0,10)<='"+fksxto+"'";
                    }else{
                        sqlfield=sqlfield+"-1,";
                    }
                }else{
                    sqlfield=sqlfield+fksx+",-1,-1";
                    if("sqlserver".equals(DBType)){
                        sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(t.modedatacreatedate,0,10)");
                    }else{
                        sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(t.modedatacreatedate,0,10)");
                    }
                }
            }else{
                sqlfield=sqlfield+"-1,-1,-1";
            }
            if(!"1".equals(frist)){//第一次加载不执行
                sqlwhere=sqlwhere+" and 1!=1";
            }

            log.writeLog(sqlwhere);
            //新建一个weatable
            WeaTable table = new WeaTable();
            table.setPageUID(pageUid);
            table.setPageID(pageID);
            table.setPagesize(pageSize);

            String fileds = " ids,lx,modedatacreater,lastname, '"+sqlfield+"' fieldsql,countall";
            log.writeLog(fileds);
            table.setBackfields(fileds);

            table.setSqlform(" from (select distinct modedatacreater||','||'0' ids,'0' lx,modedatacreater,t1.lastname,count(1) countall from uf_dcsxzb t left join hrmresource t1 on t.modedatacreater=t1.id  where 1=1   " +sqlwhere+" group by modedatacreater,t1.lastname"+
                             " union all " +
                             " select distinct modedatacreater||','||'1' ids,'1' lx,modedatacreater,t1.lastname,count(1) countall from uf_jcdcrw t left join hrmresource t1 on t.modedatacreater=t1.id where isnull(t.sfsrw,-1)=0 and 1=1 "+sqlwhere+" group by modedatacreater,t1.lastname)  a");
            log.writeLog(" from (select distinct modedatacreater||','||'0' ids,'0' lx,modedatacreater,t1.lastname,count(1) countall from uf_dcsxzb t left join hrmresource t1 on t.modedatacreater=t1.id  where 1=1   " +sqlwhere+" group by modedatacreater,t1.lastname"+
                    " union all " +
                    " select distinct modedatacreater||','||'1' ids,'1' lx,modedatacreater,t1.lastname,count(1) countall from uf_jcdcrw t left join hrmresource t1 on t.modedatacreater=t1.id where isnull(t.sfsrw,-1)=0 and 1=1 "+sqlwhere+" group by modedatacreater,t1.lastname)  a");
            table.setSqlwhere("");
            table.setSqlorderby(" modedatacreater,lx ");
            table.setSqlprimarykey("ids");
            table.setSqlisdistinct("false");

            table.getColumns().add(new WeaTableColumn("modedatacreater").setDisplay(WeaBoolAttr.FALSE));   //设置为不显
            table.getColumns().add(new WeaTableColumn("30%", "经办人", "lastname","lastname"));
            table.getColumns().add(new WeaTableColumn("10%", "类型", "lx","lx"));
            table.getColumns().add(new WeaTableColumn("20%", "任务总数", "countall","countall"));

            WeaTableColumn blz=new WeaTableColumn("20%", "办理中事项", "ids","ids","com.api.sw.duban.util.BbTrance.getSingleRwCount");
            blz.setOtherpara("column:fieldsql").setOtherpara2("column:countall");
            table.getColumns().add(blz);

            WeaTableColumn ybj=new WeaTableColumn("20%", "已办结事项", "ybj","ybj");

            table.getColumns().add(ybj);

            WeaTableColumn bjl=new WeaTableColumn("20%", "办结率", "bjl","bjl");

            table.getColumns().add(bjl);


            //增加右侧操作选项
            //设置check是否可用
            result.putAll(table.makeDataResult());
            result.put("hasRight", true);
            result.success();
            apidatas = result.getResultMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return apidatas;
    }
    /*
        单位 报表
     */
    public Map<String, Object> getSingleDwTablelist(Map<String,Object> params, User user) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }
        try {
            if(params!=null){
                log.writeLog(params.toString());
            }
            String sqlfield="";
            String frist=Util.null2String(params.get("frist")).trim();//0:代表第一次加载
            String dw=(String)Util.null2String(params.get("dw")).trim() ;//人员
            String lx=(String)Util.null2String(params.get("lx")).trim() ;//人员
            if("".equals(dw)){
                sqlfield=sqlfield+"-1,";
            }else{
                sqlfield=sqlfield+dw+",";
            }

            //日期
            String fksx=(String)Util.null2String(params.get("fksx")) ;//反馈时限
            String fksxfrom=(String)Util.null2String(params.get("fksxfrom")) ;//反馈时限from
            String fksxto=(String)Util.null2String(params.get("fksxto")) ;//反馈时限to



            //1 今天:2本周:3本月:4本季度:5本年:6时间区间:7上年:8:上个月

            //返回消息结构体
            WeaResultMsg result = new WeaResultMsg(false);
            RecordSet re=new RecordSet();
            String DBType=re.getDBType();
            log.writeLog("DBType"+DBType);
            String pageID = "17ecc14c-9d64-4a43-81a4-7b7c6f7928571113211";
            String pageUid = pageID + "_" + user.getUID();
            String pageSize = PageIdConst.getPageSize(pageID, user.getUID());

            String sqlwhere ="  ";
            String sqlwhere1 ="  1=1 ";
            if(!"".equals(dw)){
                if(dw.endsWith(",")){
                    dw=dw.substring(0,dw.length()-1);
                }
                sqlwhere1=sqlwhere1+" and qtdw="+dw;
            }
            if(!"".equals(fksx)){
                if("6".equals(fksx)){
                    sqlfield=sqlfield+"6,";
                    if(!"".equals(fksxfrom)){
                        sqlfield=sqlfield+fksxfrom+",";
                        sqlwhere=sqlwhere+" and substr(t.modedatacreatedate,0,10)>='"+fksxfrom+"'";
                    }else{
                        sqlfield=sqlfield+"-1,";
                    }
                    if(!"".equals(fksxto)){
                        sqlfield=sqlfield+fksxto+",";
                        sqlwhere=sqlwhere+" and substr(t.modedatacreatedate,0,10)<='"+fksxto+"'";
                    }else{
                        sqlfield=sqlfield+"-1,";
                    }
                }else{
                    sqlfield=sqlfield+fksx+",-1,-1";
                    if("sqlserver".equals(DBType)){
                        sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(t.modedatacreatedate,0,10)");
                    }else{
                        sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(t.modedatacreatedate,0,10)");
                    }
                }
            }else{
                sqlfield=sqlfield+"-1,-1,-1";
            }
            if(!"1".equals(frist)){//第一次加载不执行
                sqlwhere=sqlwhere+" and 1!=1";
            }
            if(!"".equals(lx)){
                sqlwhere1=sqlwhere1+" and lx="+lx;
            }
            log.writeLog(sqlwhere);
            //新建一个weatable
            WeaTable table = new WeaTable();
            table.setPageUID(pageUid);
            table.setPageID(pageID);
            table.setPagesize(pageSize);

            String fileds = " ids,lx,qtdw,lastname, '"+sqlfield+"' fieldsql,countall";
            log.writeLog(fileds);
            table.setBackfields(fileds);

            table.setSqlform(" from (select distinct dw||','||'0' ids,'0' lx,t.dw qtdw,t1.receiveunitname lastname,count(1) countall from uf_dbjblb t left join docreceiveunit t1 on t.dw=t1.id and 1=1 "+sqlwhere+"  group by t.dw,t1.receiveunitname"+
                    " union all " +
                    " select distinct qtdw||','||'1' ids,'1' lx,t.qtdw,t1.receiveunitname lastname,count(1) countall from uf_jcrwfk t left join docreceiveunit t1 on t.qtdw=t1.id and 1=1 "+sqlwhere+" group by t.qtdw,t1.receiveunitname)  a");
            log.writeLog(" from (select distinct dw||','||'0' ids,'0' lx,t.dw qtdw,t1.receiveunitname lastname,count(1) countall from uf_dbjblb t left join docreceiveunit t1 on t.dw=t1.id and 1=1 "+sqlwhere+"  group by t.dw,t1.receiveunitname"+
                    " union all " +
                    " select distinct qtdw||','||'1' ids,'1' lx,t.qtdw,t1.receiveunitname lastname,count(1) countall from uf_jcrwfk t left join docreceiveunit t1 on t.qtdw=t1.id and 1=1 "+sqlwhere+" group by t.qtdw,t1.receiveunitname)  a");
            table.setSqlwhere(sqlwhere1);
            table.setSqlorderby(" qtdw,lx ");
            table.setSqlprimarykey("ids");
            table.setSqlisdistinct("false");

            table.getColumns().add(new WeaTableColumn("qtdw").setDisplay(WeaBoolAttr.FALSE));   //设置为不显
            table.getColumns().add(new WeaTableColumn("30%", "单位", "lastname","lastname"));
            table.getColumns().add(new WeaTableColumn("20%", "类型", "lx","lx"));
            table.getColumns().add(new WeaTableColumn("20%", "任务总数", "countall","countall"));

            WeaTableColumn blz=new WeaTableColumn("10%", "办理中事项", "ids","ids","com.api.sw.duban.util.BbTrance.getDwRwCount");
            blz.setOtherpara("column:fieldsql").setOtherpara2("column:countall");
            table.getColumns().add(blz);


            WeaTableColumn ybj=new WeaTableColumn("10%", "已办结事项", "ybj","ybj");
            table.getColumns().add(ybj);

            WeaTableColumn bjl=new WeaTableColumn("10%", "办结率", "bjl","bjl");
            table.getColumns().add(bjl);
            //单位 新加的数据

            WeaTableColumn wqs=new WeaTableColumn("10%", "未签收", "wqs","wqs","com.api.sw.duban.util.BbTrance.getRwQs");
            wqs.setOtherpara("column:fieldsql").setOtherpara2("column:ids");
            table.getColumns().add(wqs);

            WeaTableColumn yqs=new WeaTableColumn("10%", "已签收", "yqs","yqs");

            table.getColumns().add(yqs);

            WeaTableColumn csqs=new WeaTableColumn("10%", "超时签收", "csqs","csqs","com.api.sw.duban.util.BbTrance.getRwOverTimeQs");
            csqs.setOtherpara("column:fieldsql").setOtherpara2("column:ids");
            table.getColumns().add(csqs);

            WeaTableColumn csfk=new WeaTableColumn("10%", "超时反馈", "csfk","csfk","com.api.sw.duban.util.BbTrance.getRwOverTimeFk");
            csfk.setOtherpara("column:fieldsql").setOtherpara2("column:ids");
            table.getColumns().add(csfk);

            WeaTableColumn zfk=new WeaTableColumn("10%", "总反馈", "zfk","zfk","com.api.sw.duban.util.BbTrance.getRwFkAll");
            zfk.setOtherpara("column:fieldsql").setOtherpara2("column:ids");
            table.getColumns().add(zfk);

            WeaTableColumn yq=new WeaTableColumn("10%", "延期", "yq","yq","com.api.sw.duban.util.BbTrance.getRwFkyqorcborbh");
            yq.setOtherpara("column:fieldsql").setOtherpara2("column:ids");
            table.getColumns().add(yq);

            WeaTableColumn bh=new WeaTableColumn("10%", "驳回", "bh","bh");

            table.getColumns().add(bh);

            WeaTableColumn cb=new WeaTableColumn("10%", "催办", "cb","cb");

            table.getColumns().add(cb);






            //增加右侧操作选项
            //设置check是否可用
            result.putAll(table.makeDataResult());
            result.put("hasRight", true);
            result.success();
            apidatas = result.getResultMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return apidatas;
    }
    /*
     单位 报表
  */
    public Map<String, Object> getSingleLxTablelist(Map<String,Object> params, User user) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }
        try {
            if(params!=null){
                log.writeLog(params.toString());
            }
            String sqlfield="";
            String frist=Util.null2String(params.get("frist")).trim();//0:代表第一次加载

            String lx=(String)Util.null2String(params.get("lx")).trim() ;//人员

            //日期
            String fksx=(String)Util.null2String(params.get("fksx")) ;//反馈时限
            String fksxfrom=(String)Util.null2String(params.get("fksxfrom")) ;//反馈时限from
            String fksxto=(String)Util.null2String(params.get("fksxto")) ;//反馈时限to
            //1 今天:2本周:3本月:4本季度:5本年:6时间区间:7上年:8:上个月

            //返回消息结构体
            WeaResultMsg result = new WeaResultMsg(false);
            RecordSet re=new RecordSet();
            String DBType=re.getDBType();
            log.writeLog("DBType"+DBType);
            String pageID = "17ecc14c-9d64-4a43-81a4-7b7c6f7928571113211";
            String pageUid = pageID + "_" + user.getUID();
            String pageSize = PageIdConst.getPageSize(pageID, user.getUID());

            String sqlwhere ="  1=1 ";
            String sqlwhere1 ="  1=1 ";

            if(!"".equals(fksx)){
                if("6".equals(fksx)){
                    sqlfield=sqlfield+"6,";
                    if(!"".equals(fksxfrom)){
                        sqlfield=sqlfield+fksxfrom+",";
                        sqlwhere=sqlwhere+" and substr(t.modedatacreatedate,0,10)>='"+fksxfrom+"'";
                    }else{
                        sqlfield=sqlfield+"-1,";
                    }
                    if(!"".equals(fksxto)){
                        sqlfield=sqlfield+fksxto+",";
                        sqlwhere=sqlwhere+" and substr(t.modedatacreatedate,0,10)<='"+fksxto+"'";
                    }else{
                        sqlfield=sqlfield+"-1,";
                    }
                }else{
                    sqlfield=sqlfield+fksx+",-1,-1";
                    if("sqlserver".equals(DBType)){
                        sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(t.modedatacreatedate,0,10)");
                    }else{
                        sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(t.modedatacreatedate,0,10)");
                    }
                }
            }else{
                sqlfield="-1,-1,-1";
            }
            if(!"1".equals(frist)){//第一次加载不执行
                sqlwhere=sqlwhere+" and 1!=1";
            }
            if(!"".equals(lx)){
                //sqlwhere1=sqlwhere1+" and lx="+lx;
            }
            log.writeLog(sqlwhere);
            //新建一个weatable
            WeaTable table = new WeaTable();
            table.setPageUID(pageUid);
            table.setPageID(pageID);
            table.setPagesize(pageSize);

            String fileds = " ids,lx, '"+sqlfield+"' fieldsql,countall";
            log.writeLog(fileds);
            table.setBackfields(fileds);

            table.setSqlform(" from (select distinct '0' ids,'0' lx,count(1) countall from uf_dbjblb t where  "+sqlwhere+" "+
                    " union all " +
                    " select distinct '1' ids,'1' lx,count(1) countall from uf_jcrwfk t where "+sqlwhere+" )  a");
            log.writeLog(" from (select distinct '0' ids,'0' lx,count(1) countall from uf_dbjblb t where  "+sqlwhere+" "+
                    " union all " +
                    " select distinct '1' ids,'1' lx,count(1) countall from uf_jcrwfk t where "+sqlwhere+" )  a");
            table.setSqlwhere(sqlwhere1);
            table.setSqlorderby(" lx ");
            table.setSqlprimarykey("ids");
            table.setSqlisdistinct("false");


            table.getColumns().add(new WeaTableColumn("20%", "类型", "lx","lx"));

            WeaTableColumn tzs=new WeaTableColumn("10%", "事项总数（件）", "zs","zs","com.api.sw.duban.util.BbTrance.getRwTzCount");
            tzs.setOtherpara("column:fieldsql").setOtherpara2("column:lx");
            table.getColumns().add(tzs);

            table.getColumns().add(new WeaTableColumn("20%", "任务总数", "countall","countall"));

            WeaTableColumn blz=new WeaTableColumn("10%", "办理中事项", "ids","ids","com.api.sw.duban.util.BbTrance.getLxRwCount");
            blz.setOtherpara("column:fieldsql").setOtherpara2("column:countall");
            table.getColumns().add(blz);


            WeaTableColumn ybj=new WeaTableColumn("10%", "已办结事项", "ybj","ybj");
            table.getColumns().add(ybj);

            WeaTableColumn bjl=new WeaTableColumn("10%", "办结率", "bjl","bjl");
            table.getColumns().add(bjl);



            //单位 新加的数据


            //增加右侧操作选项
            //设置check是否可用
            result.putAll(table.makeDataResult());
            result.put("hasRight", true);
            result.success();
            apidatas = result.getResultMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return apidatas;
    }
}
